package com.qyhl.webtv.basiclib.common;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import java.util.List;

import io.reactivex.disposables.Disposable;

public class MyObserver implements LifecycleObserver {

    private static final String TAG = "MyObserver";

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void connectListener() {
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void oncreateListener() {

    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void disconnectListener() {
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroyListener() {
        //页面销毁 取消网络请求
        List<Disposable> disList = DisposableList.getInstance().getDisList();
        cancel(disList);

    }

    private void cancel(List<Disposable> disList) {

        for (int i = 0; i < disList.size(); i++) {
            Disposable dis = disList.get(i);
            if (dis != null && !dis.isDisposed()) {
//                dis.dispose();
            }
        }
        DisposableList.getInstance().clearDis();
    }
}
